﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Missile : Bullet
{
    public List<GameObject> enemy_list = new List<GameObject>();
    public Vector3 attack_position;

    public override void FollowEnemy(GameObject enemy)
    {
        attack_position = enemy.transform.position;
        transform.LookAt(attack_position);
    }

    protected override void BulletAttack()
    {
        transform.Translate(Vector3.forward * Time.deltaTime * speed);
        if (Vector3.Distance(transform.position, attack_position) < 0.3f)
        {
            for (int i = 0; i < enemy_list.Count; i++)
            {
                if (enemy_list[i] != null)
                {
                    enemy_list[i].GetComponent<Enemy>().GetHurt(damage);
                }
            }
            BulletDestroy();
        }
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.tag == "enemy")
        {
            enemy_list.Add(other.gameObject);
        }
    }
    private void OnTriggerExit(Collider other)
    {
        if (other.tag == "enemy")
        {
            enemy_list.Remove(other.gameObject);
        }
    }
}
